package com.codeOfDoom.Sets.Managers.DeckManager
{
	import com.codeOfDoom.Sets.SetsCard.ICard;
	import com.codeOfDoom.Sets.SetsCard.SetsCard;
	import com.codeOfDoom.Sets.SetsDeck.IDeck;
	import com.codeOfDoom.Sets.SetsDeck.SetsDeck;
	
	import mx.collections.ArrayCollection;

	public class DeckManager implements IDeckManager
	{
		private var _deck:SetsDeck;
		public function DeckManager(deck:IDeck)
		{			
			_deck = deck as SetsDeck;
			shuffle();			
		}

		public function set deck(deck:SetsDeck):void
		{
			_deck = deck;
		}
		
		public function removeCard():void
		{
			if (_deck.length > 0)
				_deck.removeItemAt(0);
		}
		//refactor this name. 
		public function replaceCards(total:int):ArrayCollection
		{
			var cards:ArrayCollection = new ArrayCollection();
			if (_deck.length >= total){
				for(var i:int=0;i<total;i++){					
					cards.addItem(new SetsCard(_deck.getItemAt(i) as ICard));
				}
				for(i=0;i<total;i++){
					removeCard();
				}
			}else{
				//add remaining cards
				total = _deck.length
				for each (var card:ICard in _deck){
					cards.addItem(new SetsCard(card));
				}
				for(i=0;i<total;i++){
					removeCard();
				}
			}
			return cards;
		}
		public function shuffle():void{
			for(var i:int=0;i<_deck.length;i++){
				var tmp:ICard=_deck[i] as ICard;
		      	var randomNum:int=Math.floor(Math.random()*_deck.length);
		      	_deck[i]=_deck[randomNum] as ICard;
		      	_deck[randomNum]=tmp;
		  	}
		}
		
		
	}
}